package com.code.common.web.interceptors;

import com.code.common.constants.HeaderConstants;
import com.code.common.enums.CallSourceEnum;
import com.code.common.enums.ResultCode;
import com.code.common.exceptions.BusinessException;
import com.code.common.util.StringUtil;
import com.code.common.util.VersionUtil;
import com.code.common.web.annotations.ExcelExport;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * HEADER头参数校验
 * Header头参数大小写不敏感，比如参数X-Token和X-TOEKN是一个参数
 *
 * @author tangyifei
 */
@Slf4j
public class HeaderParamsCheckInterceptor implements HandlerInterceptor {

    private static final String MIN_API_VERSION = "1.0.0";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            ExcelExport excelExport = method.getAnnotation(ExcelExport.class);
            if (null != excelExport) {
                return true;
            }

            log.info("请求接口地址：{}", request.getRequestURL().toString());

            String callSource = request.getHeader(HeaderConstants.CALL_SOURCE);
            String apiVersion = request.getHeader(HeaderConstants.API_VERSION);
            String appVersion = request.getHeader(HeaderConstants.APP_VERSION);
            if (StringUtil.isAnyBlank(callSource, apiVersion)) {
                log.info("调用来源、API版本号不能为空");
                throw new BusinessException(ResultCode.PARAM_NOT_COMPLETE);
            }

            if (VersionUtil.compareVersion(apiVersion, MIN_API_VERSION) < 0) {
                throw new BusinessException(ResultCode.API_VERSION_TOO_LOW);
            }

            if (CallSourceEnum.ANDROID.name().equals(callSource) || CallSourceEnum.IOS.name().equals(callSource)) {
                if (StringUtil.isEmpty(appVersion)) {
                    log.info("APP版本号不能为空");
                    throw new BusinessException(ResultCode.PARAM_NOT_COMPLETE);
                }
                if (VersionUtil.compareVersion(appVersion, MIN_API_VERSION) < 0) {
                    throw new BusinessException(ResultCode.APP_VERSION_TOO_LOW);
                }
            }

            if (!CallSourceEnum.isValid(callSource)) {
                log.info("非法的调用来源");
                throw new BusinessException(ResultCode.PARAM_IS_INVALID);
            }
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        // nothing to do
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        // nothing to do
    }
}
